import {checkPhone, handlerGobackClick, hint} from "../../../utils/util";
import User from "../../../models/user";
const user = new User()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone:"",
    code:"",
    codeText:"获取验证码",
    isDisabled:false,
    registerStatus:''

  },
  onLoad(options){
    this.setData({
      phone:options.phone
    })
  },
  inputPhone(e){
    this.setData({
      phone:e.detail.value
    })
  },
  inputCode(e){
    this.setData({
      code:e.detail.value
    })
  },
  handlerGobackClick,
  authCodeLogin(){
    wx.redirectTo({
      url:"/owner-app/pages/pw-login/index"
    })
  },
  goPwLogin(){
    wx.redirectTo({
      url:"/owner-app/pages/pw-login/index"
    })
  },
  pwLogin(){
    let phone = checkPhone(this.data.phone)
    if (phone !== true) {
      hint(phone)
      return
    }
    if(this.data.code === ""){
      hint("请输入验证码")
      return;
    }
    user.appCheckCode(this.data.phone,this.data.code)
      .then((res) => {
          console.log(this.data.registerStatus,"看当前的");
          if(this.data.registerStatus === 0){
            wx.navigateTo({
              url:'/owner-app/pages/input-pw-pw/index?phone='+this.data.phone
            })
            return;
          }
          if(this.data.registerStatus === 1){
            user.appCheckLogin(this.data.phone,this.data.code)
                .then((res)=>{
                  if(res.code === 1000){
                    wx.setStorageSync("token",res.messageBody.token)
                    wx.setStorageSync("user",res.messageBody.user)
                    wx.switchTab({
                      url:"/pages/home/index"
                    })
                  }
                })
                .catch((err)=>{
                  console.log(err);})
            return;
          }
        })
        .catch((err)=>{
          console.log(err);})
  },
  getCode(){
    let phone = checkPhone(this.data.phone)
    if (phone !== true) {
      hint(phone)
      return
    }
    if(!this.data.isDisabled){
      user.registerGetAuthCode(this.data.phone,2)
        .then((res) => {
            console.log(res, "当前的");
            this.setData({
              registerStatus:res.result.registerStatus
            })
            if(res.result.registerStatus === 0){
              hint("验证码发送成功")
              this.setTime()
              return
            }if (res.result.registerStatus === 1){
              hint("验证码发送成功")
              this.setTime()
              return;
            }})
          .catch((err)=>{
            console.log(err);})
    }else {
      wx.showToast({title:"请勿频繁点击",icon:'none',duration:2000})
    }
  },
  setTime(){
    let time = 60;
    let i = 0;
    let that = this;
    //1.在60秒之内，button上倒计时60秒，并显示
    //2.向controller请求验证码，并发送到指定的手机号上面
    let set = setInterval(()=> {
      this.setData({
        codeText:(--time)+"秒"
      })
      i++;
      //此变量用于标识只调用一次获取验证码的函数
      if (i==1) {
        //使获取验证码按钮不可点击
        this.setData({
          isDisabled:true
        })
      }
    }, 1000);

    //60秒之后需要做的事情
    setTimeout(()=> {
      //恢复获取验证码按钮可点击
      this.setData({
        isDisabled:false,
        codeText:"获取验证码",
      })
      clearInterval(set);//清除计时器
    }, time * 1000); //60000
  }
})
